#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QGridLayout>
#include <QSettings>
#include <QMessageBox>

#include "settingsdialog.h"
#include "pop3invoker.h"
#include "settingsmanager.h"

SettingsDialog::SettingsDialog(QWidget *parent) :
    QDialog(parent)
{
    createWidgets();
    createLayout();
    createConnections();

    setWindowTitle(tr("Settings"));
}

void SettingsDialog::createWidgets()
{
    playerEmailLabel = new QLabel(tr("Player E-mail:"),this);
    passwordLabel = new QLabel(tr("Password:"),this);
    pop3ServerLabel = new QLabel(tr("POP3 Server:"),this);
    smtpServerLabel = new QLabel(tr("SMTP Server:"),this);
    ownerEmailLabel = new QLabel(tr("Owner E-mail:"),this);

    playerEmailEdit = new QLineEdit(this);
    playerEmailEdit->setText(SettingsManager::GetPlayerEmail());
    passwordEdit = new QLineEdit(this);
    passwordEdit->setEchoMode(QLineEdit::Password);
    passwordEdit->setText(SettingsManager::GetPasssword());
    pop3ServerEdit = new QLineEdit(this);
    pop3ServerEdit->setText(SettingsManager::GetPOP3Server());
    smtpServerEdit = new QLineEdit(this);
    smtpServerEdit->setText(SettingsManager::GetSMTPServer());
    ownerEmailEdit = new QLineEdit(this);
    ownerEmailEdit->setText(SettingsManager::GetOwnerEmail());

    playerEmailLabel->setBuddy(playerEmailEdit);
    passwordLabel->setBuddy(passwordEdit);
    pop3ServerLabel->setBuddy(pop3ServerEdit);
    smtpServerLabel->setBuddy(smtpServerEdit);
    ownerEmailLabel->setBuddy(ownerEmailEdit);
    okButton = new QPushButton(tr("OK"), this);
    cancelButton = new QPushButton(tr("Cancel"), this);
}

void SettingsDialog::createLayout()
{
    QGridLayout *editLayout = new QGridLayout;
    editLayout->addWidget(playerEmailLabel,0,0);
    editLayout->addWidget(playerEmailEdit,0,1);
    editLayout->addWidget(passwordLabel,1,0);
    editLayout->addWidget(passwordEdit,1,1);
    editLayout->addWidget(pop3ServerLabel,2,0);
    editLayout->addWidget(pop3ServerEdit,2,1);
    editLayout->addWidget(smtpServerLabel,3,0);
    editLayout->addWidget(smtpServerEdit,3,1);
    editLayout->addWidget(ownerEmailLabel,4,0);
    editLayout->addWidget(ownerEmailEdit,4,1);

    QVBoxLayout *leftLayout = new QVBoxLayout;
    leftLayout->addLayout(editLayout);

    QVBoxLayout *rightLayout  = new QVBoxLayout;
    rightLayout->addWidget(okButton);
    rightLayout->addWidget(cancelButton);

    QHBoxLayout *mainLayout = new QHBoxLayout(this);
    mainLayout->addLayout(leftLayout);
    mainLayout->addLayout(rightLayout);
    setLayout(mainLayout);
}

void SettingsDialog::createConnections()
{
    connect(okButton,SIGNAL(clicked()),this,SLOT(accept()));
    connect(cancelButton,SIGNAL(clicked()),this,SLOT(reject()));
}

QString SettingsDialog::GetPlayerEmail()
{
    return playerEmail;
}

QString SettingsDialog::GetPasssword()
{
    return password;
}

QString SettingsDialog::GetPOP3Server()
{
    return pop3Server;
}

QString SettingsDialog::GetSMTPServer()
{
    return smtpServer;
}

QString SettingsDialog::GetOwnerEmail()
{
    return ownerEmail;
}

void SettingsDialog::accept()
{
    playerEmail = playerEmailEdit->text();
    password = passwordEdit->text();
    pop3Server = pop3ServerEdit->text();
    smtpServer = smtpServerEdit->text();
    ownerEmail = ownerEmailEdit->text();

    POP3Invoker receiveInvoker;
    if(receiveInvoker.Connect(pop3Server) && receiveInvoker.Login(playerEmail,password))
    {
        SettingsManager::SetPlayerEmail(playerEmail);
        SettingsManager::SetPasssword(password);
        SettingsManager::SetPOP3Server(pop3Server);
        SettingsManager::SetSMTPServer(smtpServer);
        SettingsManager::SetOwnerEmail(ownerEmail);
        return QDialog::accept();
    }
    else
    {
        QMessageBox::critical(this,"Error","Can not connect to the mail server.",QMessageBox::Yes,QMessageBox::Yes);
    }
}
